package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

public class OnlineIDEAdmin {
        private List<OnlineIDEAdminBean> usersList = new ArrayList<OnlineIDEAdminBean>();
        private String userCount="0";
        private String selectedUser;
        private UserDetails userDetails;

                public String getUserCount() {
                try{
                        Properties props = new Properties();
                        props.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"));
                        
                        Set keySet = props.keySet();
                        if(keySet.size()>0){
                                userCount=String.valueOf(keySet.size()-1);
                        }
                }catch(Exception e){
                        e.printStackTrace();
                }
                return userCount;
        }

        public void setUserCount(String userCount) {
                this.userCount = userCount;
        }

        public List<OnlineIDEAdminBean> getUsersList() {
                try{
                        Properties props = new Properties();
                        props.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"));
                        
                        Set keySet = props.keySet();
                        Iterator itr = keySet.iterator();
                        FolderSize folderSize = new FolderSize();
                        while(itr.hasNext()){
                                        String userName =(String)itr.next();
                                        if(userName!=null && !IDEConstants.ADMIN_USER.equalsIgnoreCase(userName)){
                                        OnlineIDEAdminBean adminBean = new OnlineIDEAdminBean();
                                        
                                        adminBean.setUserName(userName);
                                        if(userName!=null){
                                                File userFolder = new File(NewGmailUser.getSelectedContextPath()+IDEConstants.USERS_FOLDER+File.separator+"/"+userName);
                                                if(userFolder.isDirectory()){
                                                                System.out.println("folder size :"+folderSize.getDirSizeInMegabytes(userFolder));
                                                                        adminBean.setFolderSize(String.valueOf(folderSize.getDirSizeInMegabytes(userFolder))+IDEConstants.MEGA_BYTES);
                                                                        File createdFolder = new File(NewGmailUser.getSelectedContextPath()+IDEConstants.USERS_FOLDER+File.separator+userName+File.separator+IDEConstants.CREATED_FOLDER);
                                                                        if(createdFolder.isDirectory()){
                                                                        	System.out.println("OnlineIDEAdmin:getUsersList:createdFolder");
                                                                        	System.out.println("OnlineIDEAdmin:getUsersList:createdFolder.lastModified()"+createdFolder.lastModified());
                                                                        	adminBean.setLastModDate(StringUtils.getTimeInFormat(createdFolder.lastModified()));
                                                                        	adminBean.setModDate(new Date(createdFolder.lastModified()));
                                                                        }
                                                }else{
                                                         adminBean.setFolderSize("yet to write program");
                                                         adminBean.setLastModDate("mm-ddd-yyyy");
                                                         adminBean.setModDate(new Date(0000000000000));
                                                        
                                                }
                                        }
                                       usersList.add(adminBean);
                                        }
                        }
                        
                }catch(Exception e){
            e.printStackTrace();
                }
                Collections.sort(usersList,COMPARATORUSER);
                return usersList;
        }
        private static Comparator<OnlineIDEAdminBean> COMPARATORUSER = new Comparator<OnlineIDEAdminBean>(){
            public int compare(OnlineIDEAdminBean o1, OnlineIDEAdminBean o2)
            {
                return o1.getModDate().compareTo(o2.getModDate());
            }
        };
        public void setUsersList(List<OnlineIDEAdminBean> usersList) {
                this.usersList = usersList;
        }
        public String deleteUser(){
                 System.out.println("delete user ::::"+selectedUser);
                File deleteUserFile = new File(NewGmailUser.getSelectedContextPath()+IDEConstants.USERS_FOLDER+File.separator+"/"+selectedUser);
                System.out.println("abs path of delete user :::"+deleteUserFile.getAbsolutePath());
                if(deleteUserFile.exists() && deleteUserFile.isDirectory()){
                        System.out.println("file before delete :::");
                        System.out.println(StringUtils.deleteFolder(deleteUserFile));
                        System.out.println("after delete");
                }
                try{
                        Properties props = new Properties();
                props.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"));
                props.remove(selectedUser);
                props.store(new FileOutputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"),"Saving UserDetails");
                
                Properties forgetProps = new Properties();
                forgetProps.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/forGetPassword.properties"));
                
                forgetProps.remove(selectedUser);
                forgetProps.store(new FileOutputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/forGetPassword.properties"),"Saving forgetPassword");
                
                
                }catch(Exception e){
                        e.printStackTrace();
                }
                usersList.clear();
                return "goToAdminPage";
        }
        public String openUser(){
        	
        	System.out.println("OnlineIDEAdmin open user ::::"+selectedUser);
        	userDetails.setFirstTime(true);
        	userDetails.setUsername(selectedUser);
        	try{
        		Properties props = new Properties();
                props.load(new FileInputStream(NewGmailUser.getSelectedContextPath()+"SourceFolder/UserDetails.properties"));
                System.out.println("props.getProperty(username):"+props.getProperty(selectedUser));
                userDetails.setPassword(props.getProperty(selectedUser));
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        	
        	return "openUser";
        }
        public String goToLoginPage(){
                HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            session.invalidate();
                return "goToLoginPage";
        }
        public String getSelectedUser() {
                        return selectedUser;
                }

                public void setSelectedUser(String selectedUser) {
                        this.selectedUser = selectedUser;
                }

				public void setUserDetails(UserDetails userDetails) {
					this.userDetails = userDetails;
				}
}
